﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class MenuSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public MenuSer()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public decimal addorUpdate(MENUS entity)
        {
            if (entity.MENUID == 0) dbContext.MENUS.Add(entity);
            else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.MENUID;
        }

        public List<MENUS> getList(int cate)
        {
            dbContext.Configuration.ProxyCreationEnabled = false;

            try
            {
                dbContext.Configuration.ProxyCreationEnabled = false;
                return dbContext.MENUS.Where(p => p.MAINMENU == cate || cate == 0).OrderBy(p => p.STT).ToList();
            }
            catch
            {
                return null;
            }
        }

        public MENUS GetById(decimal id)
        {
            try
            {
                dbContext.Configuration.ProxyCreationEnabled = false;
                return dbContext.MENUS.Where(p => p.MENUID == id).First();
            }
            catch
            {
                return null;
            }
        }

        public bool detele(decimal id)
        {
            try
            {
                var _entity = GetById(id);
                dbContext.MENUS.Remove(_entity);
                dbContext.SaveChanges();
                return true;
            }
            catch { return false; }
        }
    }
}
